#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author：albert time:2019/9/11
# recording===>记录
from recording_1 import The_recording  #实现录音功能
from recording_2 import  recording #针对音频降噪处理
from baidu_aip_1 import The_text,Speech #语音识别、语音合成
from  recording_4 import  conversion #实现语音转换
from recording_3 import  play_audio #实现语音播放
import time,requests,json
from urllib.request import quote  #编码
from The_robot import answer  #人机交互

# 获取天气信息==》备份
# def get_weather(city):
#     city_name = quote(city)
#     appid = "92b0126092f657bd4cbddd8828b2f28d"
#     url = "https://api.shenjian.io/?appid=" + appid + "&city_name=" + city_name
#     response = requests.get(url)
#     weather = json.loads(response.text)
#     a = weather["data"]["now"][0]
#     details = [i for i in a.values()]
#     details_list = details[1] + details[4] + details[5]
#     weather_list = "".join(details_list)
#     return  weather_list
def return_text():
    '''实现录音、降噪、识别功能'''
    The_recording()  #
    time.sleep(0.5)
    recording()
    text = The_text()
    word = text["result"][0]  # 说话识别的文字
    return  word #返回识别文字


def check_The_weather(content,city_name):
    if "天气" in content:
        print('正在查询....请稍等')
        weather = "{}的天气为:".format(city_name) + answer(content,city_name)
        if_else(weather)
    else:
        city_name = ""
        # prompt====> 提示
        prompt = answer(content,city_name)
        if_else(prompt)

def if_else(response): #使回答更智障
    def rule(data):
        """语音合成"""
        Speech(data)
        conversion()  # 进行语音转换
        play_audio("speech.wav")  # 包放语音

    if "超限制" in response:
        txt = "真他妈穷，赶紧充钱"
        rule(txt)  # 传入文本
        print(response)

    else:
        rule(response)
        print(response)  # 打印天气信息

if __name__ == '__main__':
    while True:
        content = return_text() #识别语音并返回文字
        c = "关闭"
        if  c in content:
            # if_else(c)
            break
        # print(content)
        city_name = content.split("的")[0] #进行切片，提取城市名
        print(city_name)
        check_The_weather(content,city_name) #天气语音播报

